# -*- coding:utf-8 -*-
# @Time : 2021/2/25 14:36
# @Author : Administrator
# @File : netbian.py
# @Software: PyCharm
# @Motto: good good study,day day up


import requests
from parsel import Selector
from loguru import logger
from pathlib import Path
import multiprocessing
import os


def get_response(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    response.encoding = response.apparent_encoding
    return response


def get_image_url(id):
    url = f'http://www.netbian.com/desk/{id}-1920x1080.htm'
    response = get_response(url)
    selector = Selector(response.text)
    image_url = selector.xpath('//td[@align="left"]//a/@href').get()
    return image_url


def save(image_url, title):
    images_base_url = Path.cwd().parent / 'images'
    filename = images_base_url.joinpath(f'{title}.jpg')
    image_content = get_response(image_url).content
    if not os.path.exists(images_base_url):
        logger.info(f'{images_base_url}目录不存在,正在创建....')
        os.mkdir(images_base_url)
    with open(filename, mode='wb') as f:
        f.write(image_content)
        logger.info(f'正在保存：{title}')


def main(url):
    response = get_response(url)
    selector = Selector(response.text)
    image_info = selector.xpath('//div[@class="list"]//ul//li')
    for item in image_info:
        title = item.xpath('./a/@title').get()
        if title:
            pic_link = item.xpath('./a/@href').get()
            id = pic_link.replace('.htm', '').split('/')[-1]
            logger.info(f'图片的id为:{id},图片的标题为:{title}')
            image_url = get_image_url(id)
            logger.info(f'图片的下载地址为:{image_url}')
            save(image_url, title)


if __name__ == '__main__':
    for page in range(1, 6):
        if page == 1:
            p = multiprocessing.Process(target=main, args=('http://www.netbian.com/index.htm',))
            p.start()
        else:
            p = multiprocessing.Process(target=main, args=(f'http://www.netbian.com/index_{page}.htm',))
            p.start()
    # images_base_url = Path.cwd().parent / 'images'
    # title='测试'
    # filename = images_base_url.joinpath(f'{title}.jpg')
    # print(filename)